﻿using System.ComponentModel.DataAnnotations;

namespace RuoVea.ExUtil.Validate;

/// <summary>
/// 描 述：不可空
/// </summary>
public class RequirAttribute : ValidationAttribute
{
    //private readonly int _maxWords;

    /// <summary>
    /// 构造函数
    /// </summary>
    public RequirAttribute()
    {
        this.ErrorMessage = "{0}:不能为空";
    }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="msg">异常消息</param>
    public RequirAttribute(string msg)
    {
        this.ErrorMessage = msg;
    }

    /// <summary>
    /// 证请求结果
    /// </summary>
    /// <param name="value">校验内容</param>
    /// <param name="validationContext">报错指定对象</param>
    /// <returns>证请求结果的容器</returns>
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null)
        {
            //FormatErrorMessage方法会自动使用显示的属性名称来格式化这个字符串
            var errorMessage = FormatErrorMessage(validationContext.DisplayName);
            return new ValidationResult(errorMessage);
        }
        return ValidationResult.Success;
    }
}
